/**
 * 
 */
package view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
/**
 * @author Ken
 *
 */
public class ApplicationView extends BaseView {
	public ApplicationView(Shell shell, Display display) {
		super(shell, display);
		mainMenu = new Menu(shell, SWT.BAR);
		createTaskSubMenu();
		createFileSubMenu();
		createDownloadSubMenu();
		createViewSubMenu();
		createToolbar();
		createTable();
		shell.setMenuBar(mainMenu);
	}

	void createTaskSubMenu() {
		taskHeader = new MenuItem(mainMenu, SWT.CASCADE);
		taskHeader.setText("&Tasks");
		
		taskSubMenu = new Menu(shell, SWT.DROP_DOWN);
		taskHeader.setMenu(taskSubMenu);
		
		MenuItem add = new MenuItem(taskSubMenu, SWT.PUSH);
		add.setText("&Add new download");
		MenuItem exit = new MenuItem(taskSubMenu, SWT.PUSH);
		exit.setText("E&xit");
	}

	void createFileSubMenu() {
		fileHeader = new MenuItem(mainMenu, SWT.CASCADE);
		fileHeader.setText("&File");
		
		fileSubMenu = new Menu(shell, SWT.DROP_DOWN);
		fileHeader.setMenu(fileSubMenu);
		
		MenuItem stop = new MenuItem(fileSubMenu, SWT.PUSH);
		stop.setText("&Stop Download");
		
		MenuItem remove = new MenuItem(fileSubMenu, SWT.PUSH);
		remove.setText("&Remove");
		
		MenuItem download = new MenuItem(fileSubMenu, SWT.PUSH);
		download.setText("&Donload Now");
		
		MenuItem redownload = new MenuItem(fileSubMenu, SWT.PUSH);
		redownload.setText("R&edownload");		
	}
	
	void createDownloadSubMenu() {
		downloadHeader = new MenuItem(mainMenu, SWT.CASCADE);
		downloadHeader.setText("&Downloads");
		
		downloadSubMenu = new Menu(shell, SWT.DROP_DOWN);
		downloadHeader.setMenu(downloadSubMenu);
		
		MenuItem pauseAll = new MenuItem(downloadSubMenu, SWT.PUSH);
		pauseAll.setText("&Pause All");
		
		MenuItem stopAll = new MenuItem(downloadSubMenu, SWT.PUSH);
		stopAll.setText("&Stop All");
		
		new MenuItem(downloadSubMenu, SWT.SEPARATOR);
		
		MenuItem deleteAll = new MenuItem(downloadSubMenu, SWT.PUSH);
		deleteAll.setText("&Delete All Completed");
		
		new MenuItem(downloadSubMenu, SWT.SEPARATOR);
		
		MenuItem scheduler = new MenuItem(downloadSubMenu, SWT.PUSH);
		scheduler.setText("S&cheduler");
		
		MenuItem startQueue = new MenuItem(downloadSubMenu, SWT.CASCADE);
		startQueue.setText("Start &Queue");		
		Menu startQueueMenu = new Menu(shell, SWT.DROP_DOWN);
		startQueue.setMenu(startQueueMenu);
		MenuItem startMainDownload = new MenuItem(startQueueMenu, SWT.PUSH);
		startMainDownload.setText("Start Main Download Queue");
		MenuItem startSynchQueue = new MenuItem(startQueueMenu, SWT.PUSH);
		startSynchQueue.setText("Start Sychronization Queue");
		
		MenuItem stopQueue = new MenuItem(downloadSubMenu, SWT.CASCADE);
		stopQueue.setText("S&top Queue");
		Menu stopQueueMenu = new Menu(shell, SWT.DROP_DOWN);
		stopQueue.setMenu(stopQueueMenu);
		MenuItem stopMainDownload = new MenuItem(stopQueueMenu, SWT.PUSH);
		stopMainDownload.setText("Stop Main Download Queue");
		MenuItem stopSynchQueue = new MenuItem(stopQueueMenu, SWT.PUSH);
		stopSynchQueue.setText("Stop Synchronization Queue");
		
		new MenuItem(downloadSubMenu, SWT.SEPARATOR);
		
		MenuItem speed = new MenuItem(downloadSubMenu, SWT.CASCADE);
		speed.setText("Speed &Limiter");
		Menu speedMenu = new Menu(shell, SWT.DROP_DOWN);
		speed.setMenu(speedMenu);
		MenuItem speedOn = new MenuItem(speedMenu, SWT.RADIO);
		speedOn.setText("Turn On");
		MenuItem speedOff = new MenuItem(speedMenu, SWT.RADIO);
		speedOff.setText("Turn Off");
		
		new MenuItem(speedMenu, SWT.SEPARATOR);
		
		MenuItem setting = new MenuItem(speedMenu, SWT.PUSH);
		setting.setText("S&ettings...");
		
		new MenuItem(downloadSubMenu, SWT.SEPARATOR);
		
		MenuItem option = new MenuItem(downloadSubMenu, SWT.PUSH);
		option.setText("&Options");
	}

	void createViewSubMenu() {
		viewHeader = new MenuItem(mainMenu, SWT.CASCADE);
		viewHeader.setText("&View");
		
		viewSubMenu = new Menu(shell, SWT.DROP_DOWN);
		viewHeader.setMenu(viewSubMenu);
		
		MenuItem hideCategories = new MenuItem(viewSubMenu, SWT.CHECK);
		hideCategories.setText("Hide Categories");
		
		MenuItem arrangeFilesHeader = new MenuItem(viewSubMenu, SWT.CASCADE);
		arrangeFilesHeader.setText("&Arrange Files");
		Menu arrangeFileMenu = new Menu(shell, SWT.DROP_DOWN);
		arrangeFilesHeader.setMenu(arrangeFileMenu);
		
		MenuItem byIndex = new MenuItem(arrangeFileMenu, SWT.PUSH);
		byIndex.setText("By &Order Of Addition");
		MenuItem byName = new MenuItem(arrangeFileMenu, SWT.PUSH);
		byName.setText("By &File Name");
		MenuItem bySize = new MenuItem(arrangeFileMenu, SWT.PUSH);
		bySize.setText("By &Size");
		MenuItem byStatus = new MenuItem(arrangeFileMenu, SWT.PUSH);
		byStatus.setText("By St&atus");
	}

	void createToolbar() {
		toolbar = new ToolBar(shell, SWT.HORIZONTAL);		
		GridData gridData = new GridData(GridData.FILL, SWT.CENTER, true, false, 1, 1);
		toolbar.setLayoutData(gridData);
		
		ToolItem addLink = new ToolItem(toolbar, SWT.PUSH);
		addLink.setText("Add URL");
		addLink.setImage(new Image(display, "res/app_add_link.png"));
		
		ToolItem resume = new ToolItem(toolbar, SWT.PUSH);
		resume.setText("Resume");
		resume.setImage(new Image(display, "res/app_resume.png"));		
		
		ToolItem stop = new ToolItem(toolbar, SWT.PUSH);
		stop.setText("Stop");
		stop.setImage(new Image(display, "res/app_stop.png"));
		
		ToolItem del = new ToolItem(toolbar, SWT.PUSH);
		del.setText("Delete");
		del.setImage(new Image(display, "res/app_delete.png"));
		
		ToolItem option = new ToolItem(toolbar, SWT.PUSH);
		option.setText("Option");
		option.setImage(new Image(display, "res/app_setting.png"));
		
		toolbar.pack();
	}

	void createTable() {
		table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		String[] titles = new String[] { "File Name", "Size", "Status", "Time Left", "Transfer Rate" };
		for (int i = 0; i < titles.length; i++) {
			TableColumn tableCol = new TableColumn(table, SWT.FILL);
			tableCol.setText(titles[i]);
			tableCol.setWidth(200);
		}			
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		table.setLayoutData(data);
		table.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	public AddLinkDialogView createAddURLDialog() {	
		Shell nShell = new Shell(this.shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		int offsetX, offsetY;
		Rectangle rect = shell.getBounds();
		if (rect.width > 600)
			offsetX = rect.x + (int)((rect.width - 600) * 0.5f);
		else
			offsetX = rect.x;
		if (rect.height > 120)
			offsetY = rect.y + (int)((rect.height - 120) * 0.5f);
		else
			offsetY = rect.y;
			
		nShell.setBounds(offsetX, offsetY, 600, 120);
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.marginWidth = 10;
		gridLayout.marginHeight = 10;
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 10;
		gridLayout.verticalSpacing = 5;
		nShell.setLayout(gridLayout);
		AddLinkDialogView addLink = new AddLinkDialogView(nShell, display);
		addLink.open();
		nShell.open();
		return addLink;
	}
	
	public ToolBar toolbar;
	public Menu mainMenu;
	public MenuItem taskHeader;
	public Menu taskSubMenu;
	public MenuItem fileHeader;
	public Menu fileSubMenu;
	public MenuItem downloadHeader;
	public Menu downloadSubMenu;
	public MenuItem viewHeader;
	public Menu viewSubMenu;
	public Table table;
}